function h = patchCaptures(h,captures,i,cmap,mode,varargin)
%function to create a system movement plot for the captures
%   Creates a capture history plot for the fish with captures contained in
%   the captures array at index "i".
%   input arguments
%       h - handle to the figure to update
%       captures - captures structured array, needs fields .tags and
%           .captures
%       i - index of the fish to plot
%       cmap - color map array that contains one line for each station, so
%       thats  stationcan be plotted by color
%       mode - mode is a string telling it what plot type to make, right
%       now there are only two modes, "indiv" which makes one plot for each
%       fish, and "common", which makes a shared plot for all fish;

figure(h)%target figure to match tag code. Note that hold is on, so if you want to plot figures for
%multiple detection thresholds you can if you don't close all figures
%first.
hold on
[L,w]=size(captures(i).caps);

switch mode
    case 'indiv' %this case makes a single plot for each fish
        lineX=[];lineY=[];
        for c=1:L %begin capture loop
            if rem(captures(i).caps(c,5),1)==0
                thisC=[0,0,0];
            else
                thisC=[.7,.7,.7];
            end
            
            plotX=[captures(i).caps(c,1),captures(i).caps(c,2),captures(i).caps(c,2),captures(i).caps(c,1)];
            plotY=[captures(i).caps(c,5)-.1,captures(i).caps(c,5)-.1,captures(i).caps(c,5)+.1,captures(i).caps(c,5)+.1];
%             patch(plotX,plotY,cmap(round(captures(i).caps(c,5)),:));
            patch(plotX,plotY,thisC);
            lineX=[lineX;captures(i).caps(c,1);captures(i).caps(c,2)];
            lineY=[lineY;captures(i).caps(c,5);captures(i).caps(c,5)];
        end %end capture loop
        
        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        %plot the dotted line. If you want to change the dotted line color,
        %change the plotting text argument where it says 'k'
        plot(lineX,lineY,'k:')
       
        
        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        %this is where the tick labels are set, just change the array of
        %strings;
        set(gca,'color',[1,1,1],'ytick',[1,2,3,4,5, 6, 6.01],'yticklabel',{'rkm 34','rkm 37','rkm 38','rkm 43', 'rkm 46','rkm 48',''})
       % ylabel('Downstream ---> Upstream') %change this label if you change from station to river mile
        
        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        %this section of code changes the xtick labels from julian days to
        %text, with a minimum width of 1 day.  Ticks are at midnight.
        lower=floor(min(lineX));
        upper=ceil(max(lineX));
        if upper-lower<1
            upper=upper+1;
        end
        if upper-lower+1>10
            xticks=lower:round((upper-lower)/10):upper;   
        else
            xticks=lower:1:upper;   
        end
        xlabels=cell(size(xticks));
        yl=ylim;

        for j=1:length(xticks-1);
            xlabels{j}=datestr(xticks(j)+datenum(2011, 1, 1, 0, 0, 0)-1,'mm-dd');
        end
        set(gca,'xtick',xticks,'xticklabel',xlabels);
        xlim([lower,upper]);             
        %%%% set the axis labels
        %xlabel('Date')
        title(strcat('Capture History: Tag ',num2str(captures(i).tag)))
        %%%%%%%%%%%%%%%%%%%%%% End date lables code %%%%%%%%%%%%%%%%%%%%%%
        
        
        %%%%%%%%%%%%%%%%%%%% Daylight coloring of axis%%%%%%%%%%%%%%%%%%%%
        %set(gca,'color',[1,1,1]);
        %sr=5/24;%sunrise Hour
        %ss=21/24;%sunset Hour
        %add daylight patches.
        %days=lower:1:upper;
        %for d=1:length(days)-1
            %lightX=[days(d)+sr,days(d)+ss,days(d)+ss,days(d)+sr];
            %lightY=[yl(1),yl(1),yl(2),yl(2)];
            %patch(lightX,lightY,'y','facealpha',.1,'edgecolor','none');
            %add night patches
            %dark1X=[days(d),days(d)+sr,days(d)+sr,days(d)];
            %dark2X=[days(d)+ss,days(d)+1,days(d)+1,days(d)+ss];
            %darkY=[yl(1),yl(1),yl(2),yl(2)];
            %patch(dark1X,darkY,'b','facealpha',.1,'edgecolor','none');
            %patch(dark2X,darkY,'b','facealpha',.1,'edgecolor','none');
       % end
        %%%%%%%%%%%%%%%%%%%%% End daylight section, comment out if not
        %%%%%%%%%%%%%%%%%%%%% waneted %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
            
    case 'common' %this case creates a single plot that shows the path for each fish
        lineX=[];lineY=[];
        for c=1:L %begin capture loop
            plotX=[captures(i).caps(c,1),captures(i).caps(c,2),captures(i).caps(c,2),captures(i).caps(c,1)];
            plotY=[captures(i).caps(c,5)-.1,captures(i).caps(c,5)-.1,captures(i).caps(c,5)+.1,captures(i).caps(c,5)+.1];
            if isempty(varargin)
                patch(plotX,plotY,cmap(i,:),'edgecolor','none');
            else
                patch(plotX,plotY,varargin{2},'edgecolor','none');
            end
            lineX=[lineX;captures(i).caps(c,1);captures(i).caps(c,2)];
            lineY=[lineY;captures(i).caps(c,5);captures(i).caps(c,5)];
         end %end capture loop
        if isempty(varargin)
            plot(lineX,lineY,'color',cmap(i,:),'LineStyle',':');
        else
            plot(lineX,lineY,varargin{1});
        end
        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        %this is where the tick labels are set, just change the array of
        %strings;
        set(gca,'color',[1,1,1],'ytick',[1,2,3,4,5, 6, 6.01],'yticklabel',{'Orrington','S. Brewer','S. Bangor','Old Bangor Dam', 'Graham Station','VZ Dam','VZ Fish Ladder'})
       % ylabel('Downstream  ---->  Upstream')
        
        if ~isempty(varargin)
            lower=floor(min(get(gca,'xlim')));
            upper=ceil(max(get(gca,'xlim')));
            if upper-lower<1
                upper=upper+1;
            end
            if upper-lower+1>10
                xticks=lower:round((upper-lower)/10):upper;   
            else
                xticks=lower:1:upper;   
            end
            xlabels=cell(size(xticks));
            yl=ylim;

            for j=1:length(xticks-1);
                xlabels{j}=datestr(xticks(j)+datenum(2011, 1, 1, 0, 0, 0)-1,'mm-dd');
            end
            set(gca,'xtick',xticks,'xticklabel',xlabels);
        end
end %end case structure
        

